package Unicart.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

public class ClientDataPage extends AnyPage {

	public ClientDataPage(PageManager pages) {
		super(pages);
	}

	//Тип покупателя
	@FindBy (id ="order_customer_type_juridical")
	private WebElement typeJuridicalRadioButton;
	@FindBy (id ="order_customer_type_physical")
	private WebElement typePhysicalRadioButton;
	
	//Данные покупателя (физ. лицо)
	@FindBy (id = "order_customer_name") //имя
	private WebElement orderCustomerName;
	@FindBy (id = "order_customer_surname") //фамилия
	private WebElement orderCustomerSurname;
	@FindBy (id = "order_customer_email") //email
	private WebElement orderCustomerEmail;
	@FindBy (id = "order_customer_phone") //телефон
	private WebElement orderCustomerPhone;
	@FindBy (id = "order_isLicensee") // чекбокс выбора лицензиата
	private WebElement otherLicenseOwnerCheckbox;
	
	@FindBy (id ="order_customer_rnn")
	private WebElement orderCustomerRnnForKZ;
	@FindBy (id ="order_customer_iin")
	private WebElement orderCustomerIinForKZ;
	
	//Лицензиат
	@FindBy (xpath ="//*[@id='order_licensee_type_physical']") //радио-кнопка физическое лицо
	private WebElement orderLicenseeTypePhysical;
	@FindBy (xpath ="//*[@id='order_licensee_type_juridical']") //радио-кнопка юридическое лицо 
	private WebElement orderLicenseeTypeJuridical;

	//Юр. лицо
	@FindBy (id ="order_company_name")
	private WebElement orderCompanyName;
	@FindBy (id ="order_company_inn")
	private WebElement orderCompanyInn;
	@FindBy (id ="order_company_kpp")
	private WebElement orderCompanyKpp;
	@FindBy (id ="order_company_juridicalAddress")
	private WebElement orderCompanyJuridicalAddress;
	@FindBy (id ="order_company_actualAddress")
	private WebElement orderCompanyActualAddress;
	@FindBy (id ="order_company_fax")
	private WebElement orderCompanyFax;
	
	//Адрес для доставки
	@FindBy (id ="order_address_city") 
	private WebElement orderAddressCity; 
	@FindBy (id ="order_address_postalCode")
	private WebElement orderAddressPostalCode;
	@FindBy (id ="order_address_street")
	private WebElement orderAddressStreet;
	@FindBy (id ="order_address_house")
	private WebElement orderAddressHouse;
	@FindBy (id ="order_address_apartment")
	private WebElement orderAddressApartment;
		
	//Блок способов оплаты
	@FindBy (xpath ="//*[@id='order_paymentSystemId']")
	private WebElement paymentMethod;
	
	//Согласие на обработку персональных данных
	@FindBy (id ="order_personalDataConfirmed")
	private WebElement orderPersonalDataConfirmed;
	
	@FindBy(xpath ="//*[@value='Оформить заказ']")
	private WebElement goToStep3Button;
	
	@FindBy(xpath ="//div[@class='b-note b-note_error']")
	private WebElement errorText;
	@FindBy(xpath ="//span[contains(text(),'"+"необходимо выбрать платежную систему"+"']")
	private WebElement paymentMethodErrorMessage;
		  
	
	public ClientDataPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.xpath("//div[.='Данные покупателя']")));
		return this;
	}
	
	public ClientDataPage clickJuridicalTypeRadioButton() {
		typeJuridicalRadioButton.click();
		return this;
	}
	
	public ClientDataPage clickPhysicalTypeRadioButton() {
		typePhysicalRadioButton.click();	
		return this;
	}
	
	public ClientDataPage setOrderCustomerName(String text) {
		orderCustomerName.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCustomerSurname(String text) {
		orderCustomerSurname.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCustomerEmail (String text) {
		orderCustomerEmail.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCustomerPhone(String text) {
		orderCustomerPhone.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderAddressCity(String text) {
		orderAddressCity.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderAddressPostalCode(String text) {
		orderAddressPostalCode.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderAddressStreet(String text) {
		orderAddressStreet.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderAddressHouse(String text) {
		orderAddressHouse.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderAddressApartment(String text) {
		orderAddressApartment.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCustomerRnn(String text) {
		orderCustomerRnnForKZ.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCustomerIin(String text) {
		orderCustomerIinForKZ.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyName(String text) {
		orderCompanyName.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyInn(String text) {
		orderCompanyInn.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyKpp(String text) {
		orderCompanyKpp.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyJuridicalAddress(String text) {
		orderCompanyJuridicalAddress.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyActualAddress(String text) {
		orderCompanyActualAddress.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage setOrderCompanyFax(String text) {
		orderCompanyFax.sendKeys(text);
	    return this;
	}
	
	public ClientDataPage clickOrderLicenseeTypeJuridical() {
		orderLicenseeTypeJuridical.click();
		return this;
	}
	
	public ClientDataPage clickOtherLicenseOwnerCheckbox() {
		otherLicenseOwnerCheckbox.click();
		return this;
	}
	
	private Select paymentMethodDropdown() {
	    return new Select(driver.findElement(By.id("order_paymentSystemId")));
	}
	
	public ClientDataPage setPaymentMethod(String text) {
		paymentMethodDropdown().selectByVisibleText(text);
		return this;
	}
	
	public ClientDataPage clickCheckboxOrderPersonalDataConfirmed() {
		orderPersonalDataConfirmed.click();
		return this;
	}
	
	public SuccessOrderingPage clickGoToStep3Button() {
		goToStep3Button.click();
		return pages.successOrderingPage;
	}
	
	public boolean errorMessageIsPresent() {
		return
			driver.findElements(By.xpath("//div[@class='b-note b-note_error']")).size() > 0;
	}

	public boolean errorMessageIsPresentPaymentMethodBlock() {
		return
			driver.findElements(By.xpath("//select[@class='b-payment-methods_select js-order-payment-system-id b-select b-input_error']")).size() > 0;
	}
	
}
